﻿/**
 * ...
 * @author Sergey Mamyan
 */

package tx.squirrel.neko.core.model;

import haxe.Md5;
import neko.db.Object;
import tx.facebook.core.Facebook;
import tx.utils.Utils;

class User extends Object {
	
	public static var manager = new UserManager();
	
	public static function get(userId:String):User {
		var user:User = manager.getById(userId);
		if (user == null) {
			user = new User();
			user.id = userId;
			user.insert();
		}
		return user;
	}
	
	public static function register(userId:String):User {
		var user:User = get(userId);
		user.isRegistered = true;
		user.update();
		return user;
	}
	
	public var id:String;
	public var name:String;
	public var username:String;
	public var url:String;
	public var picSquare:String;
	public var picBig:String;
	public var picSmall:String;
	public var friendsHash:String;
	
	public var isRegistered:Bool;
	
	public var createTime:Date;
	public var modifyTime:Date;
	public var loginTime:Date;
	
	public function new() {
		super();
		createTime 	= Date.now();
		modifyTime 	= Date.now();
		loginTime 	= Date.now();
		friendsHash = "";
	}
	
	override public function update():Void {
		if ((Date.now().getTime() - modifyTime.getTime()) > 1000 * 60) {
			this.load();
		}
		super.update();
	}
	
	override public function insert():Void {
		this.load();
		super.insert();
	}
	
	private function load():Void {
		var result:Array<Dynamic> = Facebook.fql.query(
			"SELECT name, username, url, pic_square, pic_big, pic_small FROM profile WHERE id=" + id
		);
		var userFBData:Dynamic = result.pop();
		this.name		= userFBData.name;
		this.url		= userFBData.url;
		this.username 	= userFBData.username;
		this.picSquare	= userFBData.pic_square;
		this.picBig		= userFBData.pic_big;
		this.picSmall	= userFBData.pic_small;
		this.modifyTime = Date.now();
	}
	
	public function updateFriends(friends:Array < String > ):Void {
		if (friends == null || friends.length == 0) {
			Friend.clear(this.id);
			return;
		}
		friends.sort(Utils.arraySortStandard);
		var newFriendsHash:String = Md5.encode(friends.join(","));
		if (friendsHash == newFriendsHash) {
			return;
		}else {
			this.friendsHash = newFriendsHash;
		}
		if (friendsHash == "d41d8cd98f00b204e9800998ecf8427e") {
			Friend.clear(this.id);
			return;
		}
		var result:Array<Dynamic> = Facebook.fql.query(
			"SELECT id, name, username, url, pic_square, pic_big, pic_small FROM profile WHERE id IN ("+friends.join(",")+")"
		);
		var ri:Iterator<Dynamic> = result.iterator();
		Friend.clear(this.id);
		while (ri.hasNext()) {
			var userFBData:Dynamic = ri.next();
			var friend:User 	= User.get(userFBData.id);
			friend.name			= userFBData.name;
			friend.url			= userFBData.url;
			friend.username 	= userFBData.username;
			friend.picSquare	= userFBData.pic_square;
			friend.picBig		= userFBData.pic_big;
			friend.picSmall		= userFBData.pic_small;
			friend.update();
			Friend.add(id,friend.id);
		}
		update();
	}
	
	override public function toString():String {
		return "User { \n\t name : " + name + "\n}";
	}
}

class UserManager extends neko.db.Manager < User > {
	
	public function new () {
		super(User);
	}
	
	public function getById( userId:String ) : User {
		return object(select("id='"+userId+"'"), true);
	}
	
}